home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993 October: Windmill on DISC / ADC Developer CD (1993-10) (''Windmill On DISC'')_iso / Dev.CD Oct 93.iso / System Software / U.S. System Software / System 7 Pro™ Beta 11 / Development Tools / Sample Code / Standard Mail / CollaboDraw (w⁄DigiSign) / myevents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  11.4 KB  |  570 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * Simple Sample AOCE Application Framework
  4.  *
  5.  * ©1991-1993 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * myevents.c -- main event loop and basic event handling
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        04/21/93        1.0b2        update to b2
  14.  * SJF        03/01/93        1.0b1        added digital signatures
  15.  * SJF        02/09/93        1.0b1        update to b1
  16.  * SJF        10/13/92        1.0d4        update to a11
  17.  * SJF        09/09/92        1.0d3        update to a9
  18.  * SJF        05/07/92        1.0d2        update to a6
  19.  * SJF        11/06/91        1.0d1        initial coding
  20.  *
  21.  */
  22.  
  23. #ifndef __APPLEEVENTS__
  24. #include <AppleEvents.h>
  25. #endif
  26.  
  27. #ifndef __DESK__
  28. #include <Desk.h>
  29. #endif
  30.  
  31. #ifndef __TOOLUTILS__
  32. #include <ToolUtils.h>
  33. #endif
  34.  
  35. #include "const.h"
  36. #include "mytypes.h"
  37. #include "mymenus.h"
  38. #include "globals.h"
  39. #include "utils.h"
  40. #include "myaoce.h"
  41. #include "windowstuff.h"
  42. #include "commands.h"
  43. #include "aevt.h"
  44. #include "windutils.h"
  45. #include "digisig.h"
  46.  
  47. #include "myevents.h"
  48.  
  49. /* main event loop */
  50.  
  51. void MainLoop(void)
  52. {
  53.     EventRecord ev;
  54.     Boolean gotEvent;
  55.     Point mousePt;
  56.     
  57.     gCursorRgn = NewRgn();
  58.     
  59.     while (!gDone) {
  60.         if (gHasWaitNextEvent)
  61.             gotEvent = WaitNextEvent(everyEvent,&ev,SleepTime(),gCursorRgn);
  62.         else {
  63.             gotEvent = GetNextEvent(everyEvent,&ev);
  64.             SystemTask();
  65.         }
  66.         
  67.         ProcessEvent(&ev);
  68.         
  69.         if (gotEvent) {
  70.             GetMouse(&mousePt);
  71.             LocalToGlobal(&mousePt);
  72.             HandleFixCursor(mousePt,gCursorRgn);
  73.         }
  74.         
  75.         if (gMenusDirty) {
  76.             DrawMenuBar();
  77.             gMenusDirty = false;
  78.         }
  79.     }
  80.     
  81.     DisposeRgn(gCursorRgn);
  82. }
  83.  
  84.  
  85. /* calculate how long to "wait" */
  86.  
  87. long SleepTime(void)
  88. {
  89.     if (gInBackground)
  90.         return kSleepBackground;
  91.     else
  92.         return kSleepForeground;
  93. }
  94.  
  95.  
  96. /* set cursor shape depending on mouse location */
  97.  
  98. void HandleFixCursor(Point where,RgnHandle theRgn)
  99. {
  100.     WindowPtr window;
  101.     unsigned long crsrData[2];
  102.     
  103.     if (!gInBackground) {
  104.         window = FrontWindow();
  105.         
  106.         if (IsAppWindow(window)) {
  107.             crsrData[0] = *((unsigned long *) &where);
  108.             crsrData[1] = (unsigned long)theRgn;
  109.             SendWindowMessage(window,kFixCursorMessage,crsrData);
  110.         }
  111.         else {
  112.             // we don't want a stream of mouse moved events
  113.             SetRectRgn(theRgn,-32767,-32767,32767,32767);
  114.             SetCursor(&qd.arrow);
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120. /* idle time processing */
  121.  
  122. void HandleIdle(WindowPtr window)
  123. {
  124.     if (IsAppWindow(window))
  125.         SendWindowMessage(window,kIdleMessage,nil);
  126. }
  127.  
  128.  
  129. /* process an event gotten by MainLoop() */
  130.  
  131. void ProcessEvent(EventRecord *ev)
  132. {
  133.     void *returnResult;
  134.     WindowPtr evWindow;
  135.     
  136.     if (ev->what==updateEvt)
  137.         evWindow = (WindowPtr)(ev->message);
  138.     else
  139.         evWindow = FrontWindow();
  140.             
  141.     returnResult = SendWindowMessage(evWindow,kEventMessage,ev);
  142.     if (returnResult!=nil)
  143.         return;
  144.  
  145.     switch (ev->what) {
  146.             case mouseDown:
  147.                 HandleMouseDowns(ev);
  148.                 break;
  149.             case keyDown:
  150.             case autoKey:
  151.                 HandleKeyDowns(ev);
  152.                 break;
  153.             case updateEvt:
  154.                 HandleUpdates((WindowPtr)ev->message);
  155.                 break;
  156.             case activateEvt:
  157.                 HandleActivates(ev);
  158.                 break;
  159.             case osEvt:
  160.                 HandleSREvt(ev->where,ev->message);
  161.                 break;
  162.             case kHighLevelEvent:
  163.                 DoHighLevelEvent(ev);
  164.                 break;
  165.             case nullEvent:
  166.                 HandleIdle(FrontWindow());
  167.                 break;
  168.     }
  169. }
  170.  
  171.  
  172. /* Handles suspend and resume events */
  173.  
  174. void HandleSREvt(Point where,long message)
  175. {
  176.     extern NMRec *gNotify;
  177.     extern Boolean gInBackground;
  178.     unsigned long whatMessage;
  179.     
  180.     whatMessage = message >> 24;
  181.     
  182.     if (whatMessage==suspendResumeMessage) {
  183.         if ((message & 1) != 0) {
  184.             gInBackground = false;
  185.             SetCursor(&qd.arrow);
  186.             if (FrontWindow()) {
  187.                 HiliteWindow(FrontWindow(),true);
  188.                 DoActivate(FrontWindow(),true);
  189.             }
  190.         }
  191.         else if (FrontWindow()) {
  192.             gInBackground = true;
  193.             HiliteWindow(FrontWindow(),false);
  194.             DoDeActivate(FrontWindow(),true);
  195.         }
  196.     }
  197.     else if ((whatMessage&mouseMovedMessage)==mouseMovedMessage)
  198.         HandleFixCursor(where,gCursorRgn);
  199. }
  200.  
  201.  
  202. /* Handles activate and deactivate events for a window */
  203.  
  204. void HandleActivates(EventRecord *ev)
  205. {
  206.     if ((ev->modifiers & activeFlag) != 0) {
  207.         DoActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
  208.     }
  209.     else {
  210.         DoDeActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
  211.     }
  212. }
  213.  
  214.  
  215. /* Handles activate events for a window */
  216.  
  217. void DoActivate(WindowPtr window,Boolean chFlag)
  218. {
  219.     SendWindowMessage(window,kActivateMessage,&chFlag);
  220. }
  221.  
  222.  
  223. /* Handles deactivate events for a window */
  224.  
  225. void DoDeActivate(WindowPtr window,Boolean chFlag)
  226. {
  227.     SendWindowMessage(window,kDeactivateMessage,&chFlag);
  228. }
  229.  
  230.  
  231. /* handles update events for a window */
  232.  
  233. void HandleUpdates(WindowPtr window)
  234. {
  235.     GrafPtr savePort;
  236.     
  237.     GetPort(&savePort);
  238.     SetPort(window);
  239.     BeginUpdate(window);
  240.     EraseRect(&window->portRect);
  241.     SendWindowMessage(window,kUpdateMessage,nil);
  242.     EndUpdate(window);
  243.     SetPort(savePort);
  244. }
  245.  
  246.  
  247. /* handles program keydowns */
  248.  
  249. void HandleKeyDowns(EventRecord *ev)
  250. {
  251.     short theChar;
  252.     WindowPtr window;
  253.     
  254.     theChar = ev->message & charCodeMask;
  255.     if ((ev->modifiers & cmdKey) != 0)
  256.         DoMenuCommand(MenuKey(theChar));
  257.     else if (IsAppWindow(window=FrontWindow()))
  258.         SendWindowMessage(window,kKeyMessage,&theChar);
  259. }
  260.  
  261.  
  262. /* handles mouse down events for a window */
  263.  
  264. void HandleMouseDowns(EventRecord *ev)
  265. {
  266.     WindowPtr window;
  267.     short part;
  268.     
  269.     part = FindWindow(ev->where,&window);
  270.     
  271.     switch (FindWindow(ev->where,&window)) {
  272.         case inMenuBar:
  273.             DoMenuCommand(MenuSelect(ev->where));
  274.             break;
  275.         case inSysWindow:
  276.             SystemClick(ev,window);
  277.             break;
  278.         case inDrag:
  279.             DoDrag(window,ev->where);
  280.             break;
  281.         case inGrow:
  282.             DoGrow(window,ev->where);
  283.             break;
  284.         case inGoAway:
  285.             if (TrackGoAway(window,ev->where)) {
  286.                 CommCloseWindow(window);
  287.             }
  288.             break;
  289.         case inZoomIn:
  290.         case inZoomOut:
  291.             if (TrackBox(window,ev->where,FindWindow(ev->where,&window)))
  292.                 DoZoom(window,FindWindow(ev->where,&window));
  293.             break;
  294.         case inContent:
  295.             DoContentClick(window,ev);
  296.             break;
  297.     }
  298. }
  299.  
  300.  
  301. /* handles window drag events */
  302.  
  303. void DoDrag(WindowPtr window,Point globMouse)
  304. {
  305.     Rect dragRect = kWindowDragLimits;
  306.         
  307.     DragWindow(window,globMouse,&dragRect);
  308.     SetPort(window);
  309. }
  310.  
  311.  
  312. /* handles window grow events */
  313.  
  314. void DoGrow(WindowPtr window,Point globMouse)
  315. {
  316.     long newSize;
  317.     Rect windLimits;
  318.     Rect oldSize;
  319.     GrafPtr tempPort;
  320.     WInfoHndl infoHndl;
  321.     
  322.     if (!IsAppWindow(window))
  323.         return;
  324.         
  325.     infoHndl = GetWindowInfo(window);
  326.     windLimits = (**((**infoHndl).printRecord)).prInfo.rPage;
  327.     windLimits.right += windLimits.left+kScrollBarWidth+1+(**infoHndl).leftIndent;
  328.     windLimits.bottom += windLimits.top+kScrollBarWidth+1+(**infoHndl).topIndent;
  329.     windLimits.left = 100;
  330.     windLimits.top = 100;
  331.     
  332.     oldSize = window->portRect;
  333.     if ((newSize = GrowWindow(window,globMouse,&windLimits)) != 0) {
  334.         GetPort(&tempPort);
  335.         SetPort(window);
  336.         SizeWindow(window,LoWord(newSize),HiWord(newSize),true);
  337.         InvalRect(&window->portRect);
  338.         SendWindowMessage(window,kResizeMessage,&oldSize);
  339.         SetPort(tempPort);
  340.     }
  341. }
  342.  
  343.  
  344. /* handles window zooms */
  345.  
  346. // note: this stuff is a little off: the stdState can't grow any bigger when it needs to
  347. // (when the user changes the page setup for example).  I didn't spend the time to put in
  348. // a "real" zoom feature, because it doesn't show off any of the AOCE features
  349. //
  350. void DoZoom(WindowPtr window,short zoomDir)
  351. {
  352.     GrafPtr savePort;
  353.     WInfoPtr infoPtr;
  354.     char hState;
  355.     Rect oldSize;
  356.     short newWidth,newHeight;
  357.     WStateData **stateHndl;
  358.     Rect userState,stdState;
  359.     
  360.     GetPort(&savePort);
  361.     infoPtr = BeginWindowAccess(window,&hState);
  362.     SetPort(window);
  363.  
  364.     oldSize = window->portRect;
  365.     stateHndl = (WStateData **) ((WindowPeek)window)->dataHandle;        
  366.     userState = (**stateHndl).userState;
  367.     stdState = (**stateHndl).stdState;
  368.     
  369.     ClipPageSize(&stdState,infoPtr,&newWidth,&newHeight);
  370.     stdState.right = stdState.left+newWidth;
  371.     stdState.bottom = stdState.top+newHeight;
  372.     (**stateHndl).stdState = stdState;
  373.     
  374.     ZoomWindow(window,zoomDir,true);
  375.     EraseRect(&window->portRect);
  376.         
  377.     InvalRect(&window->portRect);
  378.     SendWindowMessage(window,kResizeMessage,&oldSize);
  379.  
  380.     EndWindowAccess(window,hState);
  381.     SetPort(savePort);    
  382. }
  383.  
  384.  
  385. /* handles a click in a window content region */
  386.  
  387. void DoContentClick(WindowPtr window,EventRecord *ev)
  388. {
  389.     short part;
  390.     Point mousePos;
  391.     ControlHandle hitControl;
  392.     ControlHitMessage ctrlMessage;
  393.     
  394.     if (window != FrontWindow()) {
  395.         SelectWindow(window);
  396.         return;
  397.     }
  398.     
  399.     mousePos = ev->where;
  400.     SetPort(window);
  401.     GlobalToLocal(&mousePos);
  402.     part = FindControl(mousePos,window,&hitControl);
  403.     
  404.     if (hitControl) {
  405.         ctrlMessage.part = part;
  406.         ctrlMessage.control = hitControl;
  407.         ctrlMessage.ev = ev;
  408.         SendWindowMessage(window,kHitControlMessage,&ctrlMessage);
  409.     }
  410.     else
  411.         SendWindowMessage(window,kClickMessage,ev);        
  412. }
  413.  
  414.  
  415. /* handles menu commands */
  416.  
  417. void DoMenuCommand(long mResult)
  418. {
  419.     short selItem,selMenu,temp;
  420.     Str255 name;
  421.     GrafPtr tempPort;
  422.     MenuHandle theMenu;
  423.     
  424.     selItem = LoWord(mResult);
  425.     selMenu = HiWord(mResult);
  426.     switch (selMenu) {
  427.         case kAppleMenu:
  428.             if (selItem>2) {
  429.                 GetPort(&tempPort);
  430.                 SetCursor(&qd.arrow);
  431.                 theMenu = GetMHandle(kAppleMenu);
  432.                 GetItem(theMenu,selItem,name);
  433.                 temp = OpenDeskAcc(name);
  434.                 SetPort(tempPort);
  435.             }
  436.             else CommAbout();
  437.             break;
  438.         case kFileMenu:
  439.             switch (selItem) {
  440.                 case kNewItem:
  441.                     CommNew();
  442.                     break;
  443.                 case kOpenItem:
  444.                     CommOpen();
  445.                     break;
  446.                 case kCloseItem:
  447.                     CommCloseWindow(FrontWindow());
  448.                     break;
  449.                 case kSaveItem:
  450.                     CommSaveFile(FrontWindow());
  451.                     break;
  452.                 case kSaveAsItem:
  453.                     CommSaveAsFile(FrontWindow());
  454.                     break;
  455.                 case kPageSetupItem:
  456.                     CommPageSetup(FrontWindow());
  457.                     break;
  458.                 case kPrintItem:
  459.                     CommPrint(FrontWindow());
  460.                     break;
  461.                 case kQuitItem:
  462.                     gDone = true;
  463.                     break;
  464.             }
  465.             break;
  466.         case kEditMenu:
  467.             switch (selItem) {
  468.                 case kPrefsItem:
  469.                     CommEditPreferences();
  470.                     break;
  471.                 default:
  472.                     if (selItem>kClearItem)
  473.                         CommEdit(FrontWindow(),selItem);
  474.                     else if (!(SystemEdit(selItem-1)))
  475.                         CommEdit(FrontWindow(),selItem);
  476.                     break;
  477.             }
  478.             break;
  479.         case kShapesMenu:
  480.             switch (selItem) {
  481.                 case kGroupItem:
  482.                     SendWindowMessage(FrontWindow(),kGroupMessage,nil);
  483.                     break;
  484.                 case kUngroupItem:
  485.                     SendWindowMessage(FrontWindow(),kUnGroupMessage,nil);
  486.                     break;
  487.                 default:
  488.                     theMenu = GetMHandle(kShapesMenu);
  489.                     CheckItem(theMenu,gCurrentShape,false);
  490.                     CheckItem(theMenu,selItem,true);
  491.                     gCurrentShape = selItem;
  492.                     break;
  493.             }
  494.             break;
  495.         case kMailMenu:
  496.             switch (selItem) {
  497.                 case kSendItem:
  498.                     CommSendLetter(FrontWindow());
  499.                     break;
  500.                 case kAddRemMailItem:
  501.                     CommAddRemoveMailer(FrontWindow());
  502.                     break;
  503.                 case kReplyItem:
  504.                     CommReply(FrontWindow(),false);
  505.                     break;
  506.                 case kReplyToAllItem:
  507.                     CommReply(FrontWindow(),true);
  508.                     break;
  509.                 case kForwardItem:
  510.                     CommForward(FrontWindow());
  511.                     break;
  512.                 case kOpenNextItem:
  513.                     CommAdjacentLetter();
  514.                     break;
  515.                 case kTagLetterItem:
  516.                     CommTagLetter();
  517.                     break;
  518.             }
  519.             break;
  520.         case kSignMenu:
  521.             switch (selItem) {
  522.                 case kSignItem:
  523.                     CommSign(FrontWindow());
  524.                     break;
  525.                 case kVerifyItem:
  526.                     CommVerify(FrontWindow());
  527.                     break;
  528.                 case kShowSignItem:
  529.                     CommShowSigners(FrontWindow());
  530.                     break;
  531.             }
  532.             break;
  533.      }
  534.     HiliteMenu(0);
  535. }
  536.  
  537.  
  538. OSErr HandleOpenDoc(Boolean diskForm, FSSpec *fSpec, LetterSpec *lSpec)
  539. {
  540.     WindowPtr window;
  541.     
  542.     return LoOpen(diskForm, fSpec, lSpec,true,&window);
  543. }
  544.  
  545.  
  546. OSErr HandlePrintDoc(Boolean diskForm, FSSpec *fSpec, LetterSpec *lSpec)
  547. {
  548.     OSErr err;
  549.     WindowPtr window;
  550.     
  551.     err = LoOpen(diskForm,fSpec,lSpec,false,&window);
  552.     if (err==noErr && window) {
  553.         SendWindowMessage(window,kPrintMessage,nil);
  554.         CommCloseWindow(window);
  555.     }
  556.     return noErr;
  557. }
  558.  
  559.  
  560. Boolean ExitProgram(void)
  561. {
  562.     WindowPtr window;
  563.     
  564.     while ((window=FrontWindow()) && gDone)
  565.         CommCloseWindow(window);
  566.     
  567.     return gDone;
  568. }
  569.  
  570.